// pages/trainingRecord/trainingRecord.js
import Message from "../../miniprogram_npm/tdesign-miniprogram/message/index"
var util = require('../../utils/util.js')
import {getRehabilitation,getDoctorInfo,getToppedPlan,getTodayPlanDetail,completePlan} from "../../services/_utils/api"
import {GETSTORAGE,SETSTORAGE} from "../../services/storage/storage"
Page({

  /**
   * 页面的初始数据  86400000
   */
  data: {
    value:util.formatTime(new Date()),
    visible:false,
    minDate: new Date().getTime() - 604800000,
    maxDate: new Date().getTime() + 604800000,
    btnText:null,
    rightBtnFlag:false,
    leftBtnFlag:false,
    targetData:[],//任务列表
    incompleteFlag:false,//全部未完成
    completeFlag:false, //全部完成
    emptyFlag:false
  },
  // 前一天点击事件
  beforDay(){
    const minTime = util.formatTime(new Date(this.data.minDate))
    if(this.data.value == minTime || this.data.value < minTime){
      console.log('第一天');
      this.setData({
       leftBtnFlag:true,
       rightBtnFlag:false
      })
    }else{
      this.setData({
        leftBtnFlag:false
       })
    }
   if(this.data.leftBtnFlag == false && new Date(this.data.minDate).toLocaleDateString() != this.data.value){
    // const time = new Date(this.data.value).getTime() - 86400000
    const time = new Date(this.data.value).getTime() - 86400000
    const queryTime = util.formatTime(new Date(time))
    this.setData({
      value : queryTime
    })
    this.getTodayPlan()
   
   }else{
    Message.error({
      context: this,
      offset: [20, 32],
      duration: 3000,
      content: '已经是第一天了',
    });
   }

  },
  // 后一天点击事件
  nextDay(){
    const maxTime =util.formatTime(new Date(this.data.maxDate))
    if(this.data.value == maxTime || this.data.value > maxTime){
      this.setData({
        rightBtnFlag:true,
        leftBtnFlag:false
      })
    }else{
      this.setData({
        rightBtnFlag:false
      })
    }
   if(this.data.rightBtnFlag == false){
    const time = new Date(this.data.value).getTime() + 86400000
    const queryTime =  util.formatTime(new Date(time))
    this.setData({
      value : queryTime
    })
    this.getTodayPlan()
   }else{
    Message.error({
      context: this,
      offset: [20, 32],
      duration: 3000,
      content: '后面已经没有了',
    });
   }
  },
  handleConfirm(e){
    const time = util.formatTime(new Date(e.detail.value))
    this.setData({
      value:time
    })
    this.getTodayPlan()
  },
  handleCalendar(){
    this.setData({
      visible: true
    });
  },
  
  // 查询绑定的医生
  getContent(){
    const userid = GETSTORAGE('userId')
    const memberInfoId = GETSTORAGE('recoveryMemberInfoId')
    console.log(userid,'userid');
    getDoctorInfo({userId:userid,recoveryMemberInfoId:memberInfoId}).then(res => {
      console.log(res,"绑定的医生信息");
      if(res.data.code == 200){
        SETSTORAGE('doctorId',res.data.data.id)
        this.getDoctorPlan()
      }
    })
  },
  // 查询医生是否制定计划
  getDoctorPlan(){
    const id = GETSTORAGE('doctorId')
    const medicalRecordId = GETSTORAGE('medicalRecordId')
    console.log(medicalRecordId,'=======medicalRecordId=========');
    getToppedPlan({doctorId:id,medicalRecordId:medicalRecordId}).then(res => {
      console.log(res,'医生是否制定康复计划');
      if(res.data.code == 500){
        this.setData({
          emptyFlag:true
        })
        return
      }
      if(res.data.code == 200){
        SETSTORAGE('planId',res.data.data.id)
        this.getTodayPlan()
      }
    })
  },

  // 获取当日计划
  getTodayPlan(){
    const id = GETSTORAGE('planId')
    const date = util.formatTime(new Date(this.data.value))
    getTodayPlanDetail({developRehabilitationPlanId:id,rehabilitationDate:date}).then(res =>{
      console.log(res,'res');
      if(res.data.code == 500){
        this.setData({
          emptyFlag:true
        })
        return
      }else{
        this.setData({
          emptyFlag:false
        })
      }
      console.log(res,'当日计划明细');
      let extendnums = ''
      let bendnums = ''
      let extendnum = []
      let bendnum = []
      if(res.data.data.rehabilitationPlanDetailSensorGyroscopesList && res.data.data.rehabilitationPlanDetailSensorGyroscopesList.length>0){
        extendnums = res.data.data.rehabilitationPlanDetailSensorGyroscopesList[0].extensionTimes
        bendnums = res.data.data.rehabilitationPlanDetailSensorGyroscopesList[0].flexionTimes
        SETSTORAGE('extendNums',extendnums)
        SETSTORAGE('bendNums',bendnums)
        extendnum = res.data.data.legRecoveryRecordList.filter(item => item.legAction == '伸')
        bendnum = res.data.data.legRecoveryRecordList.filter(item => item.legAction == '屈')
        SETSTORAGE('extendCompleteNums',extendnum.length)
        SETSTORAGE('bendCompleteNums',bendnum.length)
       
      }
      this.data.targetData = res.data.data.rehabilitationPlanDetailList
      // 单选
      const choiceProblemList = res.data.data.rehabilitationPlanDetailInputChoiceList
      // 文本
      const contentProblemList = res.data.data.rehabilitationPlanDetailInputContentList
      // 数字
      const numProblemList = res.data.data.rehabilitationPlanDetailInputNumList
      const sensorGyroscopesList = res.data.data.rehabilitationPlanDetailSensorGyroscopesList
      this.data.videoList = res.data.data.rehabilitationPlanDetailVideoList
      choiceProblemList.forEach(item => {
        item.choice = JSON.parse(item.choice)
        item.negativeTerm = JSON.parse(item.negativeTerm)[0]
        if(item.inputValue != '' && item.inputValue != null){
          item.inputValue = JSON.parse(item.inputValue)[0]
        }
      })
      this.data.targetData.forEach(item => {
          item.flag = false
          // 单选
          for(let i in choiceProblemList){
            if(item.id == choiceProblemList[i].rehabilitationPlanDetailId){
              item.problem = choiceProblemList[i]
            }
          }
          // 文本
          for(let i in contentProblemList){
            if(item.id == contentProblemList[i].rehabilitationPlanDetailId){

              item.problem = contentProblemList[i]
            }
          }
          // 数字
          for(let i in numProblemList){
            if(item.id == numProblemList[i].rehabilitationPlanDetailId){
              item.problem = numProblemList[i]
            }
          }
          for(let i in this.data.videoList){
            if(item.id == this.data.videoList[i].rehabilitationPlanDetailId){
              item.video = this.data.videoList[i]
              item.video.flag = -1
            }
          }
          for(let i in sensorGyroscopesList){
            if(item.id == sensorGyroscopesList[i].rehabilitationPlanDetailId){
              item.problem = sensorGyroscopesList[i]
            }
          }
          if(item.type == "SENSOR_GYROSCOPE"){
            if(extendnum.length >= extendnums && bendnum.length >= bendnums){
              const obj = {
                inputValue:'完成'
              }
              item.problem = obj
              console.log(item,this.data.targetData);
            }
          }
      })
      this.setData({
        targetData:this.data.targetData
      })
      let List = this.data.targetData.filter(item => item.problem.inputValue == null)
      let okList = this.data.targetData.filter(item => item.problem.inputValue != null)
      if(List.length == this.data.targetData.length){
        this.setData({
          incompleteFlag:true,
          completeFlag:false
        })
      }else if(okList.length == this.data.targetData.length){
        this.setData({
          incompleteFlag:false,
          completeFlag:true
        })
      }else{
        this.setData({
          incompleteFlag:false,
          completeFlag:false
        })
      }
      console.log(this.data.targetData,'targetData');
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getContent()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getContent()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})